//
//  KeyBoardViewController.m
//  CAShapeLayerDemo
//
//  Created by XFB on 2017/12/1.
//  Copyright © 2017年 ZhouCheng. All rights reserved.
//

#import "KeyBoardViewController.h"
#import "KeyboardView.h"
#import "UIView+Extension.h"
#import "Masonry.h"

@interface KeyBoardViewController ()

@property (nonatomic, strong) KeyboardView *keyboardView;
@property (nonatomic, assign) CGFloat tempKeyBoardH;
@property (nonatomic, strong) UIView *bottomView;

/// 键盘的高度
@property (nonatomic, assign) CGFloat keyboardH;

@end

@implementation KeyBoardViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *sendMsgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendMsgButton.frame = CGRectMake(100, 100, 60, 30);
    [sendMsgButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendMsgButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    sendMsgButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    sendMsgButton.backgroundColor = [UIColor lightGrayColor];
    [sendMsgButton addTarget:self action:@selector(sendMsgButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendMsgButton];

    _tempKeyBoardH = 45;
    
    // WithFrame:CGRectMake(0, ScreenHeight-45, ScreenWidth, 45)
    self.bottomView = [[UIView alloc] init];
    self.bottomView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.bottomView];
    
    // WithFrame:CGRectMake(20, 5, ScreenWidth-100, 35)
    self.keyboardView = [[KeyboardView alloc] init];
    self.keyboardView.backgroundColor = [UIColor lightGrayColor];
    self.keyboardView.placeholder = @"测试底部键盘丝丝顺滑";
    self.keyboardView.placeholderColor = [UIColor redColor];
    self.keyboardView.maxNumberOfRowsToShow = 3;

    /// 设置底部View和键盘输入框的约束
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(45);
    }];
    
    
    __weak typeof(self) weakSelf = self;
    self.keyboardView.zc_TextHeightChangeBlock = ^(NSString *text,CGFloat textHeight){
        // 文本框文字高度改变会自动执行这个【block】，可以在这【修改底部View的高度】
        // 设置底部条的高度 = 文字高度 + textView距离上下间距约束
        // 为什么添加10 ？（10 = 底部View距离上（5）底部View距离下（5）间距总和）
//        self.keyboardView.constant = textHeight + 10;
        
        NSLog(@"%@----%.f", text, textHeight);
        _tempKeyBoardH = textHeight;
        if ([text isEqualToString:@""]) {
            _tempKeyBoardH = 45;
        } else {
            weakSelf.keyboardView.height = textHeight-1;
            weakSelf.bottomView.y = (ScreenHeight-216-textHeight-50);
            weakSelf.bottomView.height = textHeight + 10;
        }
        
        /// 计算输入框的高度
    };
    [self.bottomView addSubview:self.keyboardView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

- (void)sendMsgButtonClick
{
    /// 发送信息按钮点击 输入框成为第一响应
    [self.keyboardView becomeFirstResponder];
}

- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    
    /// 258-216 = 42
    /// 1.获取键盘的frame
    CGRect endFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardH = endFrame.size.height;
    
    /// 2.获取键盘弹出时长
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    /// 3.修改底部视图距离底部的间距
    self.bottomView.y = endFrame.origin.y != screenH?(screenH-endFrame.size.height-_tempKeyBoardH):(screenH-_tempKeyBoardH);

//    [UIView animateWithDuration:duration animations:^{
//        [self.view layoutIfNeeded];
//    }];
}


-  (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
